package com.cskaoyan.market.admin.controller;

import com.cskaoyan.market.admin.service.MarketAdminCategoryService;
import com.cskaoyan.market.admin.service.impl.MarketAdminCategoryServiceImpl;

import com.cskaoyan.market.admin.vo.CategoryFatherVo;
import com.cskaoyan.market.admin.vo.CategoryListVo;
import com.cskaoyan.market.admin.vo.CategoryVo;
import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.util.HttpUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/category/*")
public class AdminCategoryController extends HttpServlet {

    private MarketAdminCategoryService marketAdminCategoryService = new MarketAdminCategoryServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/category", "");
        switch (op) {
            case "/list":
                list(req, resp);
                break;
            case "/l1":
                l1(req,resp);
                break;
            default:
                resp.setStatus(404);
        }
    }

    private void l1(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<CategoryFatherVo> categoryFatherVoList=marketAdminCategoryService.getL1();
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.okList(categoryFatherVoList)));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/category", "");
        switch (op) {
            case "/create":
                create(req, resp);
                break;
            case "/update":
                update(req,resp);
                break;
            case "/delete":
                delete(req,resp);
                break;
            default:
                resp.setStatus(404);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        Integer id = JacksonUtil.parseInteger(requestBody, "id");
        marketAdminCategoryService.deleteCategory(id);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok()));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        String desc = JacksonUtil.parseString(requestBody, "desc");
        String iconUrl = JacksonUtil.parseString(requestBody, "iconUrl");
        Integer id = JacksonUtil.parseInteger(requestBody, "id");
        String keywords = JacksonUtil.parseString(requestBody, "keywords");
        String level = JacksonUtil.parseString(requestBody, "level");
        String name = JacksonUtil.parseString(requestBody, "name");
        String picUrl = JacksonUtil.parseString(requestBody, "picUrl");
        Integer pid = JacksonUtil.parseInteger(requestBody, "pid");
        if(StringUtils.isEmpty(name)||StringUtils.isEmpty(pid.toString())){
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        marketAdminCategoryService.updateCategory(desc,iconUrl,id,keywords,level,name,picUrl,pid);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok()));
    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        String desc = JacksonUtil.parseString(requestBody, "desc");
        String iconUrl = JacksonUtil.parseString(requestBody, "iconUrl");
        String keywords = JacksonUtil.parseString(requestBody, "keywords");
        String level = JacksonUtil.parseString(requestBody, "level");
        String name = JacksonUtil.parseString(requestBody, "name");
        String picUrl = JacksonUtil.parseString(requestBody, "picUrl");
        Integer pid = JacksonUtil.parseInteger(requestBody, "pid");
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(pid.toString())) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        MarketCategory date = marketAdminCategoryService.createCategory(desc, iconUrl, keywords, level,name, picUrl, pid);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(date)));
    }


    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<CategoryListVo> categoryList = marketAdminCategoryService.getList();
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.okList(categoryList)));
    }
}
